﻿@using MUnique.OpenMU.AdminPanel.Models
@using MUnique.OpenMU.AdminPanel.Services
@using MUnique.OpenMU.DataModel.Configuration
@using MUnique.OpenMU.Interfaces
@using MUnique.OpenMU.Persistence.Json

@inject ConnectServerService serverService;

@if (this.configuration is null)
{
    <span>Loading ...</span>
}
else
{
<EditForm Model="@configuration" OnValidSubmit="OnSubmit">
    <DataAnnotationsValidator/>
    <TextField Id="descriptionInput" Label="Description" @bind-Value="@configuration.Description"/>
    <NumberField Id="clientListenerPortInput" Label="Client listener port" @bind-Value="@configuration.ClientListenerPort"/>
    <LookupField TObject=GameClientDefinition Label="Game Client" @bind-Value="@configuration.GameClientDefinition"/>
    <div>
        <label htmlFor="patchVersionInput">Patch-Version</label>
        <div id="patchVersionInput">
            <div>
                <span id="major-addon">Major</span>
            </div>
            <InputNumber id="patchVersionMajorInput" @bind-Value=@this.configuration.CurrentVersionMajor aria-describedby="major-addon"/>
            <ValidationMessage For="@(() => this.configuration.CurrentVersionMajor)"/>
        </div>
        <div>
            <div>
                <span id="minor-addon">Minor</span>
            </div>
            <InputNumber id="patchVersionMinorInput" @bind-Value=@this.configuration.CurrentVersionMinor aria-describedby="minor-addon"/>
            <ValidationMessage For="@(() => this.configuration.CurrentVersionMinor)"/>
        </div>
        <div>
            <div>
                <span id="patch-addon">Patch</span>
            </div>
            <InputNumber id="patchVersionPatchInput" @bind-Value=@this.configuration.CurrentVersionPatch aria-describedby="patch-addon"/>
            <ValidationMessage For="@(() => this.configuration.CurrentVersionPatch)"/>
        </div>
    </div>
    <div>
        <label htmlFor="patchAddressInput">Patch-Address</label>
        <div id="patchAddressInput">
            <div>
                <span id="ftp-addon">ftp://</span>
            </div>
            <InputText @bind-Value=@this.configuration.PatchAddress aria-describedby="ftp-addon"/>
        </div>
    </div>
    <BooleanField Id="disconnectOnUnknownPacketInput" Label="Disconnect on unknown packet" @bind-Value="@configuration.DisconnectOnUnknownPacket"/>
    <NumberField Id="maximumReceiveSizeInput" Label="Maximum packet size" @bind-Value="@configuration.MaximumReceiveSize"/>
    <div>
        <label htmlFor="maxConnectionsPerAddressInput">Maximum connections per address</label>
        <div id="maxConnectionsPerAddressInput">
            <div>
                <span id="connections-addon">
                    <InputCheckbox id="checkMaxConnectionsPerAddressInput" @bind-Value=@this.configuration.CheckMaxConnectionsPerAddress/>
                </span>
            </div>
            <InputNumber min="1" @bind-Value=@this.configuration.MaxConnectionsPerAddress aria-describedby="connections-addon"/>
            <ValidationMessage For="@(() => this.configuration.MaxConnectionsPerAddress)"/>
        </div>
    </div>
    <NumberField Id="maxConnectionsInput" Label="Maximum connections" @bind-Value="@configuration.MaxConnections"/>
    <NumberField Id="listenerBacklogInput" Label="Listener Backlog" @bind-Value="@configuration.ListenerBacklog"/>
    <NumberField Id="maxFtpRequestsInput" Label="Maximum ftp requests per session" @bind-Value="@configuration.MaxFtpRequests"/>
    <NumberField Id="maxIpRequestsInput" Label="Maximum address requests per session" @bind-Value="@configuration.MaxIpRequests"/>
    <NumberField Id="maxServerListRequestsInput" Label="Maximum server list requests per session" @bind-Value="@configuration.MaxServerListRequests"/>
    <NumberField Id="timeoutSecondsInput" Label="Connection timeout (seconds)" @bind-Value="@configuration.TimeoutSeconds"/>

    <ValidationSummary/>
    <div>
        <button type="submit" class="primary-button">Save</button>
        @if (this.Cancel != null)
        {
            <button type="button" onClick=@this.Cancel>Cancel</button>
        }
    </div>

</EditForm>
}
@code {
    private ConnectServerConfigurationViewItem? configuration;

    /// <summary>
    /// The connect server which should be edited.
    /// </summary>
    [Parameter]
    public IConnectServer Server { get; set; } = null!;

    /// <summary>
    /// The cancel action. If set, an additional Cancel button is provided.
    /// </summary>
    [Parameter]
    public Task? Cancel { get; set; }

    /// <inheritdoc/>
    protected override void OnInitialized()
    {
        base.OnInitialized();

        if (this.Server.Settings is IConvertibleTo<Persistence.BasicModel.ConnectServerDefinition> convertible)
        {
            var copy = convertible.Convert();
            this.configuration = new ConnectServerConfigurationViewItem(copy);
        }
    }

    /// <summary>
    /// Is executed when clicking the Submit button of the form. Saves the configuration.
    /// </summary>
    public void OnSubmit()
    {
        if (this.configuration is not null)
        {
            this.serverService.Save(this.configuration.Configuration);
        }
    }
}
